/**
 * This project create for studying propose at BUPT University. You can change or reuse this project anywhere
 */
package edu.bupt.ooad.library.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import net.miginfocom.swing.MigLayout;
import edu.bupt.ooad.library.Library;

public class BarrowPanel extends JPanel {
    
    private JFormattedTextField bookIdTextField;
    
    private JFormattedTextField patronTextField;
    
    private JTable table;

    private Library library;
    
    /**
     * Create the panel.
     */
    public BarrowPanel(Library library) {
        this.library = library;
        
        setLayout(new BorderLayout(0, 0));
        
        JPanel addPanel = new JPanel();
        add(addPanel, BorderLayout.SOUTH);
        addPanel.setLayout(new MigLayout("", "[][grow]", "[][][]"));
        
        JLabel label1 = new JLabel("Book ID:");
        addPanel.add(label1, "cell 0 0,alignx trailing");
        
        bookIdTextField = new JFormattedTextField(NumberFormat.getInstance());
        addPanel.add(bookIdTextField, "cell 1 0,growx");
        bookIdTextField.setColumns(10);
        
        JLabel label2 = new JLabel("Patron ID:");
        addPanel.add(label2, "cell 0 1,alignx trailing");
        
        patronTextField = new JFormattedTextField(NumberFormat.getInstance());
        addPanel.add(patronTextField, "cell 1 1,growx");
        patronTextField.setColumns(10);
        
        JButton submitButton = new JButton("Borrow");
        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                borrowBook();
            }
        });
        addPanel.add(submitButton, "flowx,cell 1 2");
        
        JButton btnReturn = new JButton("Return");
        btnReturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                returnBook();
            }
        });
        addPanel.add(btnReturn, "cell 1 2");
        
        table = new JTable();
        table.setModel(new BarrowTableModel(null));
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

    }
    
    private void borrowBook() {
        String bookIdString = bookIdTextField.getText();
        String patronIdSring = patronTextField.getText();
        
        if(bookIdString == null || bookIdString.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Please enter valid book ID");
            return;
        }
        
        if(patronIdSring == null || patronIdSring.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Please enter valid patron ID");
            return;
        }
        
        int bookId = Integer.parseInt(bookIdString.trim());
        int patronId = Integer.parseInt(patronIdSring.trim());
        
        try {
            library.borrowBook(patronId, bookId);
            
            JOptionPane.showMessageDialog(this, "Book borrowed!");
            
            table.setModel(new BarrowTableModel(Tools.listBorrows(library.getPatrons())));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void returnBook() {
        String bookIdString = bookIdTextField.getText();
        
        if(bookIdString == null || bookIdString.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Please enter valid book ID");
            return;
        }
        
        int bookId = Integer.parseInt(bookIdString.trim());
        
        try {
            library.returnBook(bookId);
            
            JOptionPane.showMessageDialog(this, "Book returned!");
            
            table.setModel(new BarrowTableModel(Tools.listBorrows(library.getPatrons())));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

}
